package test;

import java.util.List;

import beans.HistoryTask;
import beans.Task;
import dao.HistoryTaskDao;
import dao.TaskDao;
import utils.FileOper;
import utils.Time2Str;

public class UpdateJSON {
	   public static void main(String[] args){
		   movJSON2File(1613,1620,1607);
		  // 1442
		   updateJsonPath(1442,1443,1607);
		   updateTaskJson(1602,1620,1607);
		   //updateOldJSONPath(2);
	   }
	   public static void movJSON2File(int start,int end, int skip){
		   TaskDao taskDao = new TaskDao();
		   Time2Str.printCurrentTime();
		   int total=0;
		   for(int i=start ;i<end;i++){
			   if(i==skip)continue;
			   List<Object>list =taskDao.getTaskById(i+"");
			   if(list!=null && list.size()>0){
			   Task task =(Task) list.get(0);
			   total++;
			   FileOper.writeTaskJson("/home/beirut/Documents/taskJson/"+i+".json", task.getJson());
			   }
		   }
		   Time2Str.printCurrentTime(total);
	   }
	   public static void updateTaskJson(int start,int end, int skip){
		   TaskDao taskDao = new TaskDao();
		   Time2Str.printCurrentTime();
		   int total=0;
		   for(int i=start ;i<end;i++){
			   if(i==skip)continue;
			   List<Object>list =taskDao.getTaskById(i+"");
			   if(list!=null && list.size()>0){
			   Task task =(Task) list.get(0);
			   total++;
			   taskDao.updateJsonPath("/home/beirut/Documents/taskJson/"+i+".json", i);
			   //FileOper.writeTaskJson("/home/beirut/Documents/taskJson/"+i+".json", task.getJson());
			   }
		   }
		   Time2Str.printCurrentTime(total);
	   }
	   public static void updateJsonPath(int start, int end,int skip){
		   HistoryTaskDao taskDao = new HistoryTaskDao();
		   Time2Str.printCurrentTime();
		   int total=0;
		   for(int i=start ;i<end;i++){
			   if(i==skip)continue;
			   boolean ret =taskDao.updateJsonPath("/home/beirut/Documents/taskJson/"+i+".json", i+"");
			   Time2Str.printCurrentTime(i);
			   if(ret){
				   Time2Str.printCurrentTime(total);
			   //Task task =(Task) list.get(0);
			   total++;
			   //FileOper.writeTaskJson("/home/beirut/Documents/taskJson/"+i+".json", task.getJson());
			   }
		   }
		   Time2Str.printCurrentTime(total);
	   }
	   public static void updateOldJSONPath(int oper){
		   HistoryTaskDao taskDao = new HistoryTaskDao();
		   Time2Str.printCurrentTime();
		   List<Object> list = taskDao.selectHistTask();
		   int start=0,end =list.size();
		   int total=0;
		   Time2Str.printCurrentTime(start+"====="+end);
		   for(int i=start ;i<end;i++){
			   //if(i==skip)continue;
//			   HistoryTask task =(HistoryTask) list.get(i);
//			   if(oper==1)//
//			   FileOper.writeTaskJson("/home/beirut/Documents/taskJson/old/"+task.getTask_hash()+".json", task.getJson());
//			   else{
//			   boolean ret =taskDao.updateJsonPathBuHash("/home/beirut/Documents/taskJson/old/"+task.getTask_hash()+".json", task.getTask_hash());
//			   if(ret){
//				   //Task task =(Task) list.get(0);
//				   total++;
//				   //FileOper.writeTaskJson("/home/beirut/Documents/taskJson/"+i+".json", task.getJson());
//				   }
//			   	}
			   
		   }
		   Time2Str.printCurrentTime(total);
	   }
}
